package net.wofly.cashier.preorder.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.cashier.order.domain.Order;
import net.wofly.cashier.preorder.domain.BookMeal;
import net.wofly.cashier.preorder.service.IBookMealService;
import net.wofly.cashier.preorder.validator.BookMealValidator;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.web.BaseController;
import net.wofly.framework.util.ReturnBean;

@RestController
@RequestMapping(value = "/api/bookMeal")
@Api(description = "预定就餐（酒店模块）")
public class BookMealController extends BaseController {

	@Autowired
	IBookMealService iBookMealService;
	/**
	 * 新增预定就餐
	 * <ul>
	 * 	<li>按日自动生成预定顺序号</li>
	 * </ul>
	 *
	 * @param bookMeal
	 */
	@ApiOperation(value = "新增预定就餐")
	@ApiImplicitParams({@ApiImplicitParam(name = "bookMeal", value = "预定就餐实体", required = true, dataType = "BookMeal")})
	@RequestMapping(value = "/addBookMeal", method = {RequestMethod.POST})
	public ReturnBean<BookMeal> addBookMeal(@RequestBody BookMeal bookMeal){
		ReturnBean<BookMeal> returnBean;
		String errorMessage = BookMealValidator.validateAddBookMealParam(bookMeal);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.addBookMeal(bookMeal);
		}
		return returnBean;
	}

	/**
	 * 取消预定就餐
	 * <ul>
	 * 	<li>未付款可以直接取消</li>
	 * 	<li>己付款的，在离就餐时间24小时内的，不可以取消</li>
	 * 	<li>己付款，离就餐时间大于24小时的，可以取消</li>
	 * 	<li>顺序号废弃</li>
	 * 	<li>验证当前订单是否为当前用户所有</li>
	 * </ul>
	 *
	 * @param bookMealID
	 * @param customer
	 */
	@ApiOperation(value = "取消预定就餐")
	@ApiImplicitParams({@ApiImplicitParam(name = "bookMealID", value = "预定就餐ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "customer", value = "客户ID", required = true, dataType = "String", paramType = "form")})
	@RequestMapping(value = "/cancelBookMeal", method = {RequestMethod.POST})
	public ReturnBean<String> cancelBookMeal(String bookMealID, String customer){
		ReturnBean<String> returnBean;
		String errorMessage = BookMealValidator.validateCancelBookMealParam(bookMealID, customer);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.cancelBookMeal(bookMealID, customer);
		}
		return returnBean;
	}

	/**
	 * 获取所有预约就餐
	 * <ul>
	 * 	<li>mealTime=-1 获取所有时间，status=-1获取所有状态</li>
	 * 	<li>验证当前用户是否有权限查看所有预定就餐</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param employeeID
	 * @param mealTime
	 * @param status
	 */
	@ApiOperation(value = "获取所有预约就餐", notes = "获取所有预约就餐")
	@ApiImplicitParams({@ApiImplicitParam(name = "employeeID", value = "员工ID", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "mealTime", value = "预约就餐时间", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "预购状态", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "int", paramType = "query")})
	@RequestMapping(value = "/getAllBookMeal", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<BookMeal>> getAllBookMeal(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
															@RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize,
															String employeeID, Long mealTime, Integer status){
		ReturnBean<PageWrapper<BookMeal>> returnBean;
		String errorMessage = BookMealValidator.validateGetAllBookMealParam(employeeID, mealTime, status);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.getAllBookMeal(pageNum, pageSize, employeeID, mealTime, status);
		}
		return returnBean;
	}

	/**
	 * 获取我的预约就餐
	 * <ul>
	 * 	<li>mealTime=-1 获取所有时间，status=-1获取所有状态</li>
	 * </ul>
	 *
	 * @param customer
	 * @param mealTime
	 * @param status
	 */
	@ApiOperation(value = "获取我的预约就餐", notes = "获取我的预约就餐")
	@ApiImplicitParams({@ApiImplicitParam(name = "customer", value = "客户ID", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "mealTime", value = "预约就餐时间", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "status", value = "预购状态", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "int", paramType = "query")})
	@RequestMapping(value = "/getMyBookMeal", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<BookMeal>> getMyBookMeal(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
														   @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize,
														   String customer, Long mealTime, Integer status){
		ReturnBean<PageWrapper<BookMeal>> returnBean;
		String errorMessage = BookMealValidator.validateGetMyBookMealParam(customer, mealTime, status);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.getMyBookMeal(pageNum, pageSize, customer, mealTime, status);
		}
		return returnBean;
	}

	/**
	 * 修改就餐时间
	 * <ul>
	 * 	<li>预定提交后，只能修改联系人，电话，就餐时间</li>
	 * 	<li>验证当前客户是否当前预定所有人</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param bookMealID
	 * @param customer
	 * @param name
	 * @param mobile
	 * @param mealTime
	 */
	@ApiOperation(value = "修改就餐时间")
	@ApiImplicitParams({@ApiImplicitParam(name = "bookMealID", value = "预定就餐ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "customer", value = "客户ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "name", value = "客户姓名", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "mobile", value = "预定人手机", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "mealTime", value = "就餐时间", required = true, dataType = "Long", paramType = "form")})
	@RequestMapping(value = "/modifyMealTime", method = {RequestMethod.POST})
	public ReturnBean<BookMeal> modifyMealTime(String bookMealID, String customer, String name, String mobile, Long mealTime){
		ReturnBean<BookMeal> returnBean;
		String errorMessage = BookMealValidator.validateModifyMealTimeParam(bookMealID, customer, name, mobile, mealTime);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.modifyMealTime(bookMealID, customer, name, mobile, mealTime);
		}
		return returnBean;
	}

	/**
	 * 付款
	 * <ul>
	 * 	<li>验证当前订单为当前用户所有</li>
	 * </ul>
	 *
	 * @param bookMealID
	 * @param customer
	 * @param num
	 */
	@ApiOperation(value = "付款")
	@ApiImplicitParams({@ApiImplicitParam(name = "bookMealID", value = "预定就餐ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "customer", value = "客户ID", required = true, dataType = "Integer", paramType = "form"),
			@ApiImplicitParam(name = "num", value = "支付金额", required = true, dataType = "Integer", paramType = "form")})
	@RequestMapping(value = "/payBookMeal", method = {RequestMethod.POST})
	public ReturnBean<String> payBookMeal(String bookMealID, String customer, Double num){
		ReturnBean<String> returnBean;
		String errorMessage = BookMealValidator.validatePayBookMealParam(bookMealID, customer, num);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.payBookMeal(bookMealID, customer, num);
		}
		return returnBean;
	}

	/**
	 * 就餐
	 * <ul>
	 * 	<li>修改预定就餐状态</li>
	 * 	<li>自动生成一条新订单</li>
	 * 	<li>验证当前用户是否有权限操作</li>
	 * </ul>
	 *
	 * @param bookMealID
	 * @param employeeID
	 */
	@ApiOperation(value = "就餐")
	@ApiImplicitParams({@ApiImplicitParam(name = "bookMealID", value = "预定就餐ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String", paramType = "form")})
	@RequestMapping(value = "/repast", method = {RequestMethod.POST})
	public ReturnBean<Order> repast(String bookMealID, String employeeID){
		ReturnBean<Order> returnBean;
		String errorMessage = BookMealValidator.validateRepastParam(bookMealID, employeeID);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.repast(bookMealID, employeeID);
		}
		return returnBean;
	}

	/**
	 * 取消预定就餐确认
	 * <ul>
	 * 	<li>确认后，将己支付费用退回到原支付帐户</li>
	 * 	<li>判断当前用户是否有确认权限</li>
	 * </ul>
	 *
	 * @param bookMealID
	 * @param employeeID
	 */
	@ApiOperation(value = "取消预定就餐确认")
	@ApiImplicitParams({@ApiImplicitParam(name = "bookMealID", value = "预定就餐ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "employeeID", value = "员工ID", required = true, dataType = "String", paramType = "form")})
	@RequestMapping(value = "/verifyCancelBookMeal", method = {RequestMethod.POST})
	public ReturnBean<BookMeal> verifyCancelBookMeal(String bookMealID, String employeeID){
		ReturnBean<BookMeal> returnBean;
		String errorMessage = BookMealValidator.validateVerifyCancelBookMealParam(bookMealID, employeeID);
		if (errorMessage.length() > 0) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iBookMealService.verifyCancelBookMeal(bookMealID, employeeID);
		}
		return returnBean;
	}

}
